package xyz.xiangyushui.dao.user.member;

import java.util.Date;
import java.util.List;
import java.util.Map;

import xyz.xiangyushui.dao.Dao;
import xyz.xiangyushui.javaBean.membership.Membership;

public class MembershipDao extends Dao{
	private final int firstMemberCardId=100000;
	private final long validGap=31536000000L; //1年
	private final double dangerousBalance=200.0;
	
	@SuppressWarnings("unchecked")
	public double getBalance(String userName){
		double result=0.0;
		List<Object> list=db.excuteQuery("select balance from membership where userName=?", new Object[]{userName});
		if(!list.isEmpty()){
			if(((Map<String,Object>)list.get(0)).get("balance")!=null){
				result=(double)((Map<String,Object>)list.get(0)).get("balance");
			}
		}
		return result;
	}
	
	
	//0是暂停状态，只能看到充值页面
	//1是正常状态,可以访问所有页面
	//2是停止状态，不能登陆。
	public int getState(String userName){
		return getIntValues("state", userName);
	}
	
	public int getRatings(String userName){
		return getIntValues("rating", userName);
	}
	
	/**
	 * 正常状态,可以访问所有页面
	 * @param userName
	 */
	public void updateStateOK(String userName){
		updateState(userName, 1);
	}
	
	/**
	 * 暂停状态，只能看到充值页面
	 */
	public void updateStatePause(String userName){
		updateState(userName, 0);
	}
	
	private void updateState(String userName,int state){
		db.executeUpdate("update membership set state=? where userName=?", new Object[]{state,userName});
	}
	
	public int getPoints(String userName){
		return getIntValues("points", userName);
	}
	
	public int getMemberCardId(String userName){
		return getIntValues("cardId", userName);
	}
	
	@SuppressWarnings("unchecked")
	private int getIntValues(String fieldName,String userName){
		int result=0;
		List<Object> list=db.excuteQuery("select "+fieldName+" from membership where userName=?", new Object[]{userName});
		if(!list.isEmpty()){
			if(((Map<String,Object>)list.get(0)).get(fieldName)!=null){
				result=(int)((Map<String,Object>)list.get(0)).get(fieldName);
			}
		}
		return result;
	}
	
	public void changeValues(String fieldName,String userName,int value){
		db.executeUpdate("update membership set "+fieldName+"="+fieldName+"+? where userName=?", new Object[]{value,userName});
	}
	
	
	public void changeBalance(String userName,double value){
		db.executeUpdate("update membership set balance=balance+? where userName=?", new Object[]{value,userName});
	}
	
	public void changePoints(String userName,int value){
		changeValues("points", userName, value);
	}
	
	public void addMember(String userName){
		db.executeUpdate("insert into membership values(?,?,?,?,?,?,?,?)", new Object[]{userName,nextMembershipCardId(),new Date().getTime(),new Date().getTime(),0.0,0,0,0});
	}
	
	public void updateRatings(int rating,String userName){
		db.executeUpdate("update membership set rating=? where userName=? ",new Object[]{rating,userName} );
	}
	
	@SuppressWarnings("unchecked")
	private int nextMembershipCardId(){
		int result=firstMemberCardId;
		List<Object> list=db.excuteQuery("select max(cardId) from membership ", new Object[]{});
		if(list!=null){
			if(((Map<String,Object>)list.get(0)).get("max(cardId)")!=null){
				result=(int)((Map<String,Object>)list.get(0)).get("max(cardId)")+1;
			}
		}
		return result;
		
	}
	
	public Membership getMembershipInfo(String userName){
		Membership m=new Membership();
		List<Object> list=db.excuteQuery("select * from membership where userName=?", new Object[]{userName});
		if(!list.isEmpty()){
			@SuppressWarnings("unchecked")
			Map<String,Object> map=(Map<String,Object>)list.get(0);
			m.setBalance((double) map.get("balance"));
			m.setUserName((String) map.get("userName"));
			m.setCardId((int) map.get("cardId"));
			m.setPoints((int) map.get("points"));
			m.setRating((int) map.get("rating"));
			m.setRegisterTime((long) map.get("registerTime"));
			m.setState((int) map.get("state"));
		}
		return m;
	}
	
	public void delete(String userName){
		db.executeUpdate("delete from membership where userName=?", new Object[]{userName});
	}
	
	public void updateStateToPause(){
		int i=db.executeUpdate("update membership set state=?,setPauseTime=? where state=? and balance<? and registerTime<?", new Object[]{0,new Date().getTime(),1,dangerousBalance,new Date().getTime()-validGap});
		System.out.println(i+"名会员状态被置为暂停。");
	}
	
	public void updateStateToStop(){
		int i=db.executeUpdate("update membership set state=? where state=? and setPauseTime<?", new Object[]{2,0,new Date().getTime()-validGap});
		System.out.println(i+"名会员状态被置为停止。");
	}
	
	
}
